unit materialOrdenCompra;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, Grids, DBGrids, RXDBCtrl, StdCtrls, Buttons, Mask, ToolEdit,
  Provider, SqlExpr, DB, DBClient, DBLocal, DBLocalS, DateUtils;

type
  TfrmMaterialOrdenCompra = class(TForm)
    lblFechaIni: TLabel;
    edtFechaIni: TDateEdit;
    lblFechaFin: TLabel;
    edtFechaFin: TDateEdit;
    chbTodos: TCheckBox;
    Label5: TLabel;
    edtFiltro: TEdit;
    btnDesfiltrar: TBitBtn;
    Label1: TLabel;
    identificador: TEdit;
    grdMaterial: TRxDBGrid;
    btnVistaPrevia: TBitBtn;
    btnCargar: TBitBtn;
    cds: TSQLClientDataSet;
    s: TDataSource;
    cdsRAZON: TStringField;
    cdsMATERIAL: TStringField;
    cdsCANTIDAD: TFloatField;
    btnExcel: TSpeedButton;
    procedure btnCargarClick(Sender: TObject);
    procedure FormClose(Sender: TObject; var Action: TCloseAction);
    procedure grdMaterialTitleClick(Column: TColumn);
    procedure chbTodosClick(Sender: TObject);
    procedure identificadorChange(Sender: TObject);
    procedure identificadorKeyPress(Sender: TObject; var Key: Char);
    procedure grdMaterialCellClick(Column: TColumn);
    procedure btnDesfiltrarClick(Sender: TObject);
    procedure edtFiltroKeyPress(Sender: TObject; var Key: Char);
    procedure identificadorKeyDown(Sender: TObject; var Key: Word;
      Shift: TShiftState);
    procedure edtFiltroChange(Sender: TObject);
    procedure btnExcelClick(Sender: TObject);
  private
    FnombreObra: string;
    FcolumnaSeleccionada: word;
    procedure SetnombreObra(const Value: string);
    procedure SetcolumnaSeleccionada(const Value: word);
    { Private declarations }
  published
    property nombreObra: string read FnombreObra write SetnombreObra;
    property columnaSeleccionada: word read FcolumnaSeleccionada write SetcolumnaSeleccionada;
  public
    constructor CreateNuevo(AOwner: TComponent; idObra: string);

  end;

var
  frmMaterialOrdenCompra: TfrmMaterialOrdenCompra;

implementation

uses MDatos, Recursos;

{$R *.dfm}

constructor TfrmMaterialOrdenCompra.CreateNuevo(AOwner: TComponent; idObra: string);
begin
  inherited Create(Aowner);
  self.edtFechaIni.Date := IncYear(now, -1);
  self.edtFechaFin.Date := now;
  if idObra <> '0' then
  begin
    self.identificador.Text := idObra;
    self.btnCargarClick(self);
  end;
  self.ShowModal;
end;

procedure TfrmMaterialOrdenCompra.btnCargarClick(Sender: TObject);
begin
  self.cds.Close;
  if self.identificador.Text <> '' then
  begin
    self.cds.Params[0].AsString := self.identificador.Text;
    self.cds.Params[2].AsString := FormatDateTime('mm/dd/yy', incDay(StrToDate(self.edtFechaFin.EditText)));
    if self.chbTodos.Checked then
      self.cds.Params[1].AsString := '01/01/1900'
    else
      self.cds.Params[1].AsString := FormatDateTime('mm/dd/yy', StrToDate(self.edtFechaIni.EditText));
    self.cds.Open;
  end
  else
    Application.MessageBox('Debe ingresar el Identificador de Obra', 'Dato Faltante',
    mb_OK+mb_ICONERROR);
end;

procedure TfrmMaterialOrdenCompra.FormClose(Sender: TObject;
  var Action: TCloseAction);
begin
  self.cds.Close;
  Action := caFree;
end;

procedure TfrmMaterialOrdenCompra.grdMaterialTitleClick(Column: TColumn);
begin
  self.cds.IndexFieldNames := Column.FieldName;
end;

procedure TfrmMaterialOrdenCompra.chbTodosClick(Sender: TObject);
begin
  self.edtFechaIni.Enabled := not self.chbTodos.Checked;
  self.edtFechaFin.Enabled := not self.chbTodos.Checked;
end;

procedure TfrmMaterialOrdenCompra.identificadorChange(Sender: TObject);
var
  ds : TSQLDataSet;
begin
  self.btnCargar.Enabled := self.identificador.Text <> '';
  self.btnVistaPrevia.Enabled := self.btnCargar.Enabled;
  if self.identificador.Text <> '' then
  begin
    ds := TSQLDataSet.Create(self);
    try
      ds.SQLConnection := Datos.Con;
      ds.CommandText := 'SELECT denominacion FROM presupuesto WHERE id_presupuesto = '
      +self.identificador.Text;
      ds.Open;
      if not ds.IsEmpty then
      begin
        self.Caption := 'MATERIALES ORDEN DE COMPRA POR OBRA - '+ds.fields [0].AsString;
        self.nombreObra := ds.fields [0].AsString;
      end;
    finally
      ds.Free;
    end;
  end;

end;

procedure TfrmMaterialOrdenCompra.identificadorKeyPress(Sender: TObject;
  var Key: Char);
begin
  if not ((StrScan('0123456789',Key) <> nil) or (Key = Char(VK_BACK))) then
    Key := #0;
end;

procedure TfrmMaterialOrdenCompra.SetnombreObra(const Value: string);
begin
  FnombreObra := Value;
end;

procedure TfrmMaterialOrdenCompra.grdMaterialCellClick(Column: TColumn);
var
  i : word;
begin
  if (Column.Title.Font.Color <> clRed) and (Column.Field.DataType = ftString)then
  begin
    for i := 0 to self.grdMaterial.Columns.Count-1 do
      self.grdMaterial.Columns [i].Title.Font.Color := Column.Title.Font.Color;
    Column.Title.Font.Color := clRed;
    self.columnaSeleccionada := self.grdMaterial.Col-1;
  end;
end;

procedure TfrmMaterialOrdenCompra.SetcolumnaSeleccionada(
  const Value: word);
begin
  FcolumnaSeleccionada := Value;
end;

procedure TfrmMaterialOrdenCompra.btnDesfiltrarClick(Sender: TObject);
begin
  self.edtFiltro.Text := '';
  self.edtFiltro.SetFocus;
end;

procedure TfrmMaterialOrdenCompra.edtFiltroKeyPress(Sender: TObject;
  var Key: Char);
begin
  if Key = chr(VK_RETURN) then
  begin
    self.cds.Filtered := False;
    if (self.edtFiltro.Text <> '') then
    begin
      self.cds.Filter := self.grdMaterial.Columns [self.columnaSeleccionada].FieldName+' LIKE ''%'+self.edtFiltro.Text+'%''';
      self.cds.Filtered := True;
    end;
  end;
end;

procedure TfrmMaterialOrdenCompra.identificadorKeyDown(Sender: TObject;
  var Key: Word; Shift: TShiftState);
begin
  if (Key = VK_RETURN) then
    self.btnCargarClick(Sender);
end;

procedure TfrmMaterialOrdenCompra.edtFiltroChange(Sender: TObject);
begin
  self.btnDesfiltrar.Enabled := self.edtFiltro.Text <> '';
end;

procedure TfrmMaterialOrdenCompra.btnExcelClick(Sender: TObject);
begin
  abrirEnExcel(self, self.cds, nil, nil, 'MATERIALES SOLICITADOS POR OBRA', 'INTERVALO DEFINIDO: del '
  +DateTimeToStr(self.edtFechaIni.Date)+' al '+DateTimeToStr(self.edtFechaFin.Date));
end;

end.
